<template>
  <div>
    <gs-header :title="pageTitle"/>
    <div class="page-sample-scroll">
      <div id="index-list-content" class="index-list-content">
        <scroll
          ref="scroll"
          @pullingDown="onPullingDown"
          @pullingUp="onPullingUp"
          @pullDownScroll="onPullDownScroll"
          :pullDownRefresh="pullDownRefreshObj"
          :pullUpLoad="pullUpLoadObj"
        >
          <ul ref="listGroup">
            <li v-for="group in list" :key="group.name">
              <h2 class="index-list-anchor">{{group.name}}</h2>
              <ul>
                <li
                  class="index-list-item"
                  v-for="item in group.items"
                  :key="item.name"
                >{{item.name}}</li>
              </ul>
            </li>
          </ul>
        </scroll>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import Scroll from 'example/components/scroll/scroll'
import GsHeader from 'example/components/gs-header/gs-header'
import cityData from 'example/data/index-list.json'

export default {
  name: 'PageSampleScroll',
  created () {
    let ret = []
    cityData.forEach(cityGroup => {
      let group = {}
      group.name = cityGroup.name
      group.items = []
      cityGroup.cities.forEach(city => {
        let item = {}
        item.name = city.name
        item.value = city.cityid
        group.items.push(item)
      })
      ret.push(group)
    })
    this.list = ret
  },
  data () {
    return {
      pageTitle: '下拉刷新，上拉加载',
      pullDownRefresh: true,
      pullDownThreshold: 60,
      pullDownStop: 40,
      pullDownText: '',
      pullUpload: true,
      pullUpThreshold: 0,
      pullUpText: '加载中',

      list: [],
      pageNo: 1
    }
  },
  computed: {
    pullDownRefreshObj () {
      return this.pullDownRefresh ? {
        threshold: parseInt(this.pullDownThreshold),
        stop: parseInt(this.pullDownStop),
        text: this.pullDownText
      } : false
    },
    pullUpLoadObj () {
      return this.pullUpload ? {
        threshold: parseInt(this.pullUpThreshold),
        text: this.pullUpText
      } : false
    }
  },
  components: {
    Scroll,
    GsHeader
  },
  methods: {
    onPullingDown () {
      this.pullDownText = '刷新中...'
      clearTimeout(this.pullDownTimer)
      this.pullDownTimer = setTimeout(() => {
        let random = Math.random().toFixed(5)
        this.list.unshift({
          name: '新数据 ' + random,
          items: [
            {
              name: 'grow-scroll',
              value: 1000
            }
          ]
        })
        this.pullDownText = '刷新成功'
        setTimeout(() => {
          this.forceUpdate('down')
        }, 500)
      }, 1000)
    },
    onPullingUp () {
      console.log(this.pageNo)
      if (this.pullLocked) {
        this.$nextTick(() => {
          this.pullLocked = false
          this.forceUpdate('up')
        })
        return
      }
      this.pullLocked = true
      this.pullUpText = '加载中...'
      clearTimeout(this.pullUpTimer)
      this.pullUpTimer = setTimeout(() => {
        if (this.pageNo > 2) {
          this.pullUpText = '没有更多数据'
          return
        } else {
          let random = +Math.random()
          for (let i = 0; i < 10; i++) {
            this.list.push({
              name: '西虹市 ' + random.toFixed(5) + i,
              items: [
                {
                  name: 'grow-scroll',
                  value: i
                }
              ]
            })
          }
        }
        this.forceUpdate('up')
        this.pageNo++
      }, 1000)
    },
    onPullDownScroll (pos) {
      if (pos.y > 60) {
        this.pullDownText = '释放刷新'
      } else {
        this.pullDownText = '下拉刷新'
      }
    },
    forceUpdate (type) {
      this.$refs.scroll.forceUpdate(type)
    }
  }
}
</script>

<style lang="scss">
.page-sample-scroll {
  position: relative;
  width: 100vw;
  height: calc(100vh - 45px);
  font-size: 14px;
  overflow: hidden;
  .pullDown-wrapper {
    position: absolute;
    top: 0;
    width: 100%;
    .pullDown-text {
      width: 100%;
      padding-top: 10px;
      text-align: center;
      color: rgba(0, 0, 0, 0.6);
    }
  }
  .pullUp-wrapper {
    width: 100%;
    padding: 5px 0 15px;
    .pullUp-text {
      width: 100%;
      padding-top: 10px;
      text-align: center;
      color: rgba(0, 0, 0, 0.6);
    }
  }
  .index-list-content {
    position: relative;
    height: 100%;
    background: transparent;
    z-index: 1;
    ul {
      background: white;
    }
    .index-list-title {
      padding: 14px 16px;
      font-size: 12px;
      line-height: 1.6;
      color: grey;
    }
    .index-list-anchor {
      padding: 16px 16px 10px 16px;
      font-size: 14px;
      color: #999;
      background: #f7f7f7;
    }
    .index-list-item {
      position: relative;
      height: 50px;
      line-height: 50px;
      padding: 0 16px;
      font-size: 12px;
      color: grey;
      &:last-child {
        border: none;
      }
    }
    .index-list-item_active {
      background: grey;
    }
  }
}
</style>
